package yuyicman.bitmaps 
{
	import com.edvardtoth.BitmapDistorter;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import yuyicman.debug.Debugger;
	
	/**
	 * ...
	 * @author Yuyic.
	 */
	public class BitmapSegment extends MovieClip 
	{
		
		
		
		private var _bd1			:	BitmapDistorter;
		private var _bd2			:	BitmapDistorter;
		private var _lineShape		:	Shape;
		private var _bmpd1			:	BitmapData;
		private var _bmpd2			:	BitmapData;
		
		
		public var segmentsList		:	Array;
		public var v1				:	BitmapSegment;
		public var v2				:	BitmapSegment;
		public var v3				:	BitmapSegment;
		public var v4				:	BitmapSegment;
		public var paper			:	BitmapPaper;
		public var imgRect			:	Rectangle;
		
		public var _x				:	Number;
		public var _x2				:	Number;
		public var _y				:	Number;
		public var _y2				:	Number;
		
		/////////////////////////////
		
		public var smoothOn			:	Boolean;
		
		/////////////////////////////
		
		public function BitmapSegment() 
		{
			
			_x2 = _y2 = 0;
			
			segmentsList = new Array;
			
			smoothOn = true;
			
			_lineShape = addChild(new Shape) as Shape;
			
		}
		
		public function init():void {
			
			x = _x;
			y = _y;
			
		}
		
		public function render():void {
			
			var tlX:Number, tlY:Number, trX:Number, trY:Number, blX:Number, blY:Number, brX:Number, brY:Number, 
				rad:Number, brightness:Number, yDis:Number, htY:Number, hbY:Number, tdX:Number, bdX:Number;
			
			_lineShape.graphics.clear();
			
			for each( var bs:BitmapSegment in segmentsList ) {
				
				_lineShape.graphics.lineStyle( .5, 0, .5 );
				_lineShape.graphics.moveTo( 0, 0 );
				_lineShape.graphics.lineTo( bs.x - x, bs.y - y );
				
			}
			
			if ( v1 && v2 && v3 ) {
				
				tlX = v1.x;
				trX = v2.x;
				blX = v3.x;
				brX = v4.x;
				
				tlY = v1.y;
				trY = v2.y;
				blY = v3.y;
				brY = v4.y;
				
				htY = (tlY + trY) * .5;
				hbY = (blY + brY) * .5;
				
				tdX = Math.abs( tlX - trX );
				bdX = Math.abs( blX - brX );
				
				yDis = hbY - htY;
				yDis == 0 && (yDis = 1);
				
				rad = Math.atan2( yDis, bdX - tdX );
				brightness = rad/ Math.PI - .5;
				
				if ( brightness > 0 && brightness < .4 ) {
					
					brightness += ( .2 - Math.abs(brightness-.2) ) * 2;
					
				}
				
				brightness = Math.min(brightness, 0.9);
                brightness = Math.max(brightness, -0.6);
				
				if ( _bd1 ) {
					
					
					_bd1.topLeftHandle.x = tlX;
					_bd1.topLeftHandle.y = tlY;
					
					_bd1.topRightHandle.x = trX;
					_bd1.topRightHandle.y = trY;
					
					_bd1.bottomLeftHandle.x = blX;
					_bd1.bottomLeftHandle.y = blY;
					
					_bd1.bottomRightHandle.x = brX;
					_bd1.bottomRightHandle.y = brY;
					
					if ( brX > blX ) {
						_bd1.brightness = brightness;
						_bd1.updateFrame();
						_bd1.visible = true;
					}
					else {
						_bd1.visible = false;
					}
				}
				
				if ( _bd2 ) {
					
					_bd2.topLeftHandle.x = tlX;
					_bd2.topLeftHandle.y = tlY;
					
					_bd2.topRightHandle.x = trX;
					_bd2.topRightHandle.y = trY;
					
					_bd2.bottomLeftHandle.x = blX;
					_bd2.bottomLeftHandle.y = blY;
					
					_bd2.bottomRightHandle.x = brX;
					_bd2.bottomRightHandle.y = brY;
					
					if ( blX > brX ) {
						_bd2.brightness = brightness;
						_bd2.updateFrame();
						_bd2.visible = true;
					}
					else {
						_bd2.visible = false;
					}
					
				}
				
				
			}
			
			
		}
		
		public function setBd1( target:DisplayObject ):void {
			
			var xStep:Number, yStep:Number, sW:Number, sH:Number;
			var mtx:Matrix;
			
			if ( _bd1 ) {
				removeChild( _bd1 );
				_bd1 = null;
			}
			
			if ( v1 && v2 && v3 ) {
				
				xStep = Math.floor( target.width * imgRect.x );
				yStep = Math.floor( target.height * imgRect.y );
				
				sW = Math.floor( target.width * imgRect.width );
				sH = Math.floor( target.height * imgRect.height );
				
				_bmpd1 = new BitmapData( sW, sH, false, 0 );
				mtx = new Matrix;
				mtx.translate( -xStep, -yStep );
				_bmpd1.draw( target, mtx, null, null, new Rectangle( 0, 0, sW, sH ), smoothOn );
				
				_bd1 = new BitmapDistorter( paper, 0, 0, sW, 0, 0, sH, sW, sH, _bmpd1 );
				addChild( _bd1 );
				
			}
			
		}
		
		public function setBd2( target:DisplayObject ):void {
			
			var xStep:Number, yStep:Number, sW:Number, sH:Number;
			var mtx:Matrix;
			
			if ( _bd2 ) {
				removeChild( _bd2 );
				_bd2 = null;
			}
			
			if ( v1 && v2 && v3 ) {
				
				xStep = Math.floor( target.width * imgRect.x );
				yStep = Math.floor( target.height * imgRect.y );
				sW = Math.floor( target.width * imgRect.width );
				sH = Math.floor( target.height * imgRect.height );
				
				_bmpd2 = new BitmapData( sW, sH, false, 0 );
				mtx = new Matrix;
				mtx.scale( -1, 1 );
				mtx.translate( target.width-xStep, -yStep );
				_bmpd2.draw( target, mtx, null, null, new Rectangle( 0, 0, sW, sH ), smoothOn );
				
				_bd2 = new BitmapDistorter( paper, 0, 0, sW, 0, 0, sH, sW, sH, _bmpd2 );
				addChild( _bd2 );
				
			}
			
		}
		
		
		
		
		public function setBrightness( target:DisplayObject, brightness:Number ):void
        {
            brightness *= 100;
            target.filters = [ new ColorMatrixFilter([1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0]) ];
        }
		
		public function set lineVisible( visible:Boolean ):void {
			_lineShape.visible = visible;
		}
		
		
	}

}